package com.xdwanj.servlet;

import com.xdwanj.util.ConstantsRes;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/study")
public class StudyServlet extends HttpServlet {
    /**
     * 映射地址
     */
    public static final String REQUEST_MAPPING = "study";

    /**
     * 动作变量名
     */
    public static final String METHOD = "method";
    /**
     * 查询动作
     */
    public static final String QUERY = "query";
    /**
     * 科普文章id
     */
    public static final String SYID = "syid";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter(METHOD);

        switch (method) {
            case QUERY:
                queryInfo(req, resp);
                break;

            default:
                break;
        }
    }

    /**
     * 查询科普页面相关数据，并转发
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void queryInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;
        String syid = req.getParameter(SYID);

        //TODO:需要相关的服务

        url = ConstantsRes.STUDY_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
